[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1 Low-Level Terminal Interface

This chapter describes functions that are specific to terminal devices. You can use these functions to do things like turn off input echoing; set serial line characteristics such as line speed and flow control; and change which characters are used for end-of-file, command-line editing, sending signals, and similar control functions.

Most of the functions in this chapter operate on file descriptors. @xref{Low-Level I/O}, for more information about what a file descriptor is and how to open a file descriptor for a terminal device.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.1 Identifying Terminals

The functions described in this chapter only work on files that correspond to terminal devices. You can find out whether a file descriptor is associated with a terminal by using the isatty function.

Prototypes for both isatty and ttyname are declared in the header file ‘unistd.h’.

Function: int isatty (int filedes)

This function returns 1 if filedes is a file descriptor associated with an open terminal device, and 0 otherwise.

If a file descriptor is associated with a terminal, you can get its associated file name using the ttyname function. See also the ctermid function, described in @ref{Identifying the Terminal}.

Function: char * ttyname (int filedes)

If the file descriptor filedes is associated with a terminal device, the ttyname function returns a pointer to a statically-allocated, null-terminated string containing the file name of the terminal file. The value is a null pointer if the file descriptor isn’t associated with a terminal, or the file name cannot be determined.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.2 I/O Queues

Many of the remaining functions in this section refer to the input and output queues of a terminal device. These queues implement a form of buffering within the kernel independent of the buffering implemented by I/O streams (@pxref{I/O on Streams}).

The terminal input queue is also sometimes referred to as its typeahead buffer. It holds the characters that have been received from the terminal but not yet read by any process.

The size of the terminal’s input queue is described by the _POSIX_MAX_INPUT and MAX_INPUT parameters; see @ref{Limits for Files}. If input flow control is enabled by setting the IXOFF input mode bit (see section Input Modes), the terminal driver transmits STOP and START characters to the terminal when necessary to prevent the queue from overflowing. Otherwise, input may be lost if it comes in too fast from the terminal. (This is unlikely if you are typing the input by hand!)

The terminal output queue is like the input queue, but for output; it contains characters that have been written by processes, but not yet transmitted to the terminal. If output flow control is enabled by setting the IXON input mode bit (see section Input Modes), the terminal driver obeys STOP and STOP characters sent by the terminal to stop and restart transmission of output.

Clearing the terminal input queue means discarding any characters that have been received but not yet read. Similarly, clearing the terminal output queue means discarding any characters that have been written but not yet transmitted.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.3 Two Styles of Input: Canonical or Not

POSIX systems support two basic modes of input: canonical and noncanonical.

In canonical input processing mode, terminal input is processed in lines terminated by newline ('\n'), EOF, or EOL characters. No input can be read until an entire line has been typed by the user, and the read function (@pxref{I/O Primitives}) returns at most a single line of input, no matter how many bytes are requested.

In canonical input mode, the operating system provides input editing facilities: the ERASE and KILL characters are interpreted specially to perform editing operations within the current line of text. See section Characters for Input Editing.

The constants _POSIX_MAX_CANON and MAX_CANON parameterize the maximum number of bytes which may appear in a single line of canonical input. @xref{Limits for Files}.

In noncanonical input processing mode, characters are not grouped into lines, and ERASE and KILL processing is not performed. The granularity with which bytes are read in noncanonical input mode is controlled by the MIN and TIME settings. See section Noncanonical Input.

Most programs use canonical input mode, because this gives the user a way to edit input line by line. The usual reason to use noncanonical mode is when the program accepts single-character commands or provides its own editing facilities.

The choice of canonical or noncanonical input is controlled by the ICANON flag in the c_lflag member of struct termios. See section Local Modes.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.4 Terminal Modes

This section describes the various terminal attributes that control how input and output are done. The functions, data structures, and symbolic constants are all declared in the header file ‘termios.h’.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.4.1 Terminal Mode Data Types

The entire collection of attributes of a terminal is stored in a structure of type struct termios. This structure is used with the functions tcgetattr and tcsetattr to read and set the attributes.

Data Type: struct termios

Structure that records all the I/O attributes of a terminal. The structure includes at least the following members:

tcflag_t c_iflag

A bit mask specifying flags for input modes; see Input Modes.

tcflag_t c_oflag

A bit mask specifying flags for output modes; see Output Modes.

tcflag_t c_cflag

A bit mask specifying flags for control modes; see Control Modes.

tcflag_t c_lflag

A bit mask specifying flags for local modes; see Local Modes.

cc_t c_cc[NCCS]

An array specifying which characters are associated with various control functions; see Special Characters.

The struct termios structure also contains members which encode input and output transmission speeds, but the representation is not specified. See section Line Speed, for how to examine and store the speed values.

The following sections describe the details of the members of the struct termios structure.

Data Type: tcflag_t

This is an unsigned integer type used to represent the various bit masks for terminal flags.

Data Type: cc_t

This is an unsigned integer type used to represent characters associated with various terminal control functions.

Macro: int NCCS

The value of this macro is the number of elements in the c_cc array.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.4.2 Terminal Mode Functions

Function: int tcgetattr (int filedes, struct termios *termios_p)

This function is used to examine the attributes of the terminal device with file descriptor filedes. The attributes are returned in the structure that termios_p points to.

If successful, tcgetattr returns 0. A return value of -1 indicates an error. The following errno error conditions are defined for this function:

EBADF

The filedes argument is not a valid file descriptor.

ENOTTY

The filedes is not associated with a terminal.

Function: int tcsetattr (int filedes, int when, const struct termios *termios_p)

This function sets the attributes of the terminal device with file descriptor filedes. The new attributes are taken from the structure that termios_p points to.

The when argument specifies how to deal with input and output already queued. It can be one of the following values:

TCSANOW

Make the change immediately.

TCSADRAIN

Make the change after waiting until all queued output has been written. You should usually use this option when changing parameters that affect output.

TCSAFLUSH

This is like TCSADRAIN, but also discards any queued input.

TCSASOFT

This is a flag bit that you can add to any of the above alternatives. Its meaning is to inhibit alteration of the state of the terminal hardware. It is a BSD extension; it has no effect on non-BSD systems.

If this function is called from a background process on its controlling terminal, normally all processes in the process group are sent a SIGTTOU signal, in the same way as if the process were trying to write to the terminal. The exception is if the calling process itself is ignoring or blocking SIGTTOU signals, in which case the operation is performed and no signal is sent. @xref{Job Control}.

If successful, tcsetattr returns 0. A return value of -1 indicates an error. The following errno error conditions are defined for this function:

EBADF

The filedes argument is not a valid file descriptor.

ENOTTY

The filedes is not associated with a terminal.

EINVAL

Either the value of the when argument is not valid, or there is something wrong with the data in the termios_p argument.

Although tcgetattr and tcsetattr specify the terminal device with a file descriptor, the attributes are those of the terminal device itself and not of the file descriptor. This means that the effects of changing terminal attributes are persistent; if another process opens the terminal file later on, it will see the changed attributes even though it doesn’t have anything to do with the open file descriptor you originally specified in changing the attributes.

Similarly, if a single process has multiple or duplicated file descriptors for the same terminal device, changing the terminal attributes affects input and output to all of these file descriptors. This means, for example, that you can’t open one file descriptor or stream to read from a terminal in the normal line-buffered, echoed mode; and simultaneously have another file descriptor for the same terminal that you use to read from it in single-character, non-echoed mode. Instead, you have to explicitly switch the terminal back and forth between the two modes.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.4.3 Setting Terminal Modes Properly

When you set terminal modes, you should call tcgetattr first to get the current modes of the particular terminal device, modify only those modes that you are really interested in, and store the result with tcsetattr.

It’s a bad idea to simply initialize a struct termios structure to a chosen set of attributes and pass it directly to tcsetattr. Your program may be run years from now, on systems that support members not documented in this manual. The way to avoid setting these members to unreasonable values is to avoid changing them.

What’s more, different terminal devices may require different mode settings in order to function properly. So you should avoid blindly copying attributes from one terminal device to another.

When a member contains a collection of independent flags, as the c_iflag, c_oflag and c_cflag members do, even setting the entire member is a bad idea, because particular operating systems have their own flags. Instead, you should start with the current value of the member and alter only the flags whose values matter in your program, leaving any other flags unchanged.

Here is an example of how to set one flag (ISTRIP) in the struct termios structure while properly preserving all the other data in the structure:

int
set_istrip (int desc, int value)
{
  struct termios settings;
  int result;

  result = tcgetattr (desc, &settings);
  if (result < 0)
    {
      perror ("error in tcgetattr");
      return 0;
    }
  settings.c_iflag &= ~ISTRIP;
  if (value)
    settings.c_iflag |= ISTRIP;
  result = tcgetattr (desc, &settings);
  if (result < 0)
    {
      perror ("error in tcgetattr");
      return;
   }
  return 1;
}

[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.4.4 Input Modes

This section describes the terminal attribute flags that control fairly low-level aspects of input processing: handling of parity errors, break signals, flow control, and <RET> and <LFD> characters.

All of these flags are bits in the c_iflag member of the struct termios structure. The member is an integer, and you change flags using the operators &, | and ^. Don’t try to specify the entire value for c_iflag—instead, change only specific flags and leave the rest untouched (see section Setting Terminal Modes Properly).

INPCK

If this bit is set, input parity checking is enabled. If it is not set, no checking at all is done for parity errors on input; the characters are simply passed through to the application.

Parity checking on input processing is independent of whether parity detection and generation on the underlying terminal hardware is enabled; see Control Modes. For example, you could clear the INPCK input mode flag and set the PARENB control mode flag to ignore parity errors on input, but still generate parity on output.

If this bit is set, what happens when a parity error is detected depends on whether the IGNPAR or PARMRK bits are set. If neither of these bits are set, a byte with a parity error is passed to the application as a '\0' character.

IGNPAR

If this bit is set, any byte with a framing or parity error is ignored. This is only useful if INPCK is also set.

PARMRK

If this bit is set, input bytes with parity or framing errors are marked when passed to the program. This bit is meaningful only when INPCK is set and IGNPAR is not set.

The way erroneous bytes are marked is with two preceding bytes, 377 and 0. Thus, the program actually reads three bytes for one erroneous byte received from the terminal.

If a valid byte has the value 0377, and ISTRIP (see below) is not set, the program might confuse it with the prefix that marks a parity error. So a valid byte 0377 is passed to the program as two bytes, 0377 0377, in this case.

ISTRIP

If this bit is set, valid input bytes are stripped to seven bits; otherwise, all eight bits are available for programs to read.

IGNBRK

If this bit is set, break conditions are ignored.

A break condition is defined in the context of asynchronous serial data transmission as a series of zero-value bits longer than a single byte.

BRKINT

If this bit is set and IGNBRK is not set, a break condition clears the terminal input and output queues and raises a SIGINT signal for the foreground process group associated with the terminal.

If neither BRKINT nor IGNBRK are set, a break condition is passed to the application as a single '\0' character if PARMRK is not set, or otherwise as a three-character sequence '\377', '\0', '\0'.

IGNCR

If this bit is set, carriage return characters ('\r') are discarded on input. Discarding carriage return may be useful on terminals that send both carriage return and linefeed when you type the <RET> key.

ICRNL

If this bit is set and IGNCR is not set, carriage return characters ('\r') received as input are passed to the application as newline characters ('\n').

INLCR

If this bit is set, newline characters ('\n') received as input are passed to the application as carriage return characters ('\r').

IXOFF

If this bit is set, start/stop control on input is enabled. In other words, the computer sends STOP and START characters as necessary to prevent input from coming in faster than programs are reading it. The idea is that the actual terminal hardware that is generating the input data responds to a STOP character by suspending transmission, and to a START character by resuming transmission. See section Special Characters for Flow Control.

IXON

If this bit is set, start/stop control on output is enabled. In other words, if the computer receives a STOP character, it suspends output until a START character is received. In this case, the STOP and START characters are never passed to the application program. If this bit is not set, then START and STOP can be read as ordinary characters. See section Special Characters for Flow Control.

IXANY

If this bit is set, any input character restarts output when output has been suspended with the STOP character. Otherwise, only the START character restarts output.

IMAXBEL

If this bit is set, then filling up the terminal input buffer sends a BEL character (code 007) to the terminal to ring the bell.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.4.5 Output Modes

This section describes the terminal flags and fields that control how output characters are translated and padded for display. All of these are contained in the c_oflag member of the struct termios structure.

The c_oflag member itself is an integer, and you change the flags and fields using the operators &, |, and ^. Don’t try to specify the entire value for c_oflag—instead, change only specific flags and leave the rest untouched (see section Setting Terminal Modes Properly).

Macro: int OPOST

If this bit is set, output data is processed in some unspecified way so that it is displayed appropriately on the terminal device. This typically includes mapping newline characters ('\n') onto carriage return and linefeed pairs.

If this bit isn’t set, the characters are transmitted as-is.

The following three bits are BSD features, and they have no effect on non-BSD systems. On all systems, they are effective only if OPOST is set.

Macro: int ONLCR

If this bit is set, convert the newline character on output into a pair of characters, carriage return followed by linefeed.

Macro: int OXTABS

If this bit is set, convert tab characters on output into the appropriate number of spaces to emulate a tab stop every eight columns.

Macro: int ONOEOT

If this bit is set, discard C-d characters (code 004) on output. These characters cause many dial-up terminals to disconnect.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.4.6 Control Modes

This section describes the terminal flags and fields that control parameters usually associated with asynchronous serial data transmission. These flags may not make sense for other kinds of terminal ports (such as a network connection pseudo-terminal). All of these are contained in the c_cflag member of the struct termios structure.

The c_cflag member itself is an integer, and you change the flags and fields using the operators &, |, and ^. Don’t try to specify the entire value for c_cflag—instead, change only specific flags and leave the rest untouched (see section Setting Terminal Modes Properly).

CLOCAL

If this bit is set, it indicates that the terminal is connected “locally” and that the modem status lines (such as carrier detect) should be ignored.

If this bit is not set and you call open without the O_NONBLOCK flag set, open blocks until a modem connection is established.

If this bit is not set and a modem disconnect is detected, a SIGHUP signal is sent to the controlling process group for the terminal (if it has one). Normally, this causes the process to exit; see @ref{Signal Handling}. Reading from the terminal after a disconnect causes an end-of-file condition, and writing causes an EIO error to be returned. The terminal device must be closed and reopened to clear the condition.

HUPCL

If this bit is set, a modem disconnect is generated when all processes that have the terminal device open have either closed the file or exited.

CREAD

If this bit is set, input can be read from the terminal. Otherwise, input is discarded when it arrives.

CSTOPB

If this bit is set, two stop bits are used. Otherwise, only one stop bit is used.

PARENB

If this bit is set, generation and detection of a parity bit are enabled. See section Input Modes, for information on how input parity errors are handled.

If this bit is not set, no parity bit is added to output characters, and input characters are not checked for correct parity.

PARODD

This bit is only useful if PARENB is set. If PARODD is set, odd parity is used, otherwise even parity is used.

The control mode flags also includes a field for the number of bits per character. You can use the CSIZE macro as a mask to extract the value, like this: settings.c_cflag & CSIZE.

CSIZE

This is a mask for the number of bits per character.

CS5

This specifies five bits per byte.

CS6

This specifies six bits per byte.

CS7

This specifies seven bits per byte.

CS8

This specifies eight bits per byte.

CCTS_OFLOW

If this bit is set, enable flow control of output based on the CTS wire (RS232 protocol).

CRTS_IFLOW

If this bit is set, enable flow control of input based on the RTS wire (RS232 protocol).

MDMBUF

If this bit is set, enable carrier-based flow control of output.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.4.7 Local Modes

This section describes the flags for the c_lflag member of the struct termios structure. These flags generally control higher-level aspects of input processing than the input modes flags described in Input Modes, such as echoing, signals, and the choice of canonical or noncanonical input.

The c_lflag member itself is an integer, and you change the flags and fields using the operators &, |, and ^. Don’t try to specify the entire value for c_lflag—instead, change only specific flags and leave the rest untouched (see section Setting Terminal Modes Properly).

ICANON

This bit, if set, enables canonical input processing mode. Otherwise, input is processed in noncanonical mode. See section Two Styles of Input: Canonical or Not.

ECHO

If this bit is set, echoing of input characters back to the terminal is enabled.

ECHOE

If this bit is set, echoing indicates erasure of input with the ERASE character by erasing the last character in the current line from the screen. Otherwise, the character erased is re-echoed to show what has happened (suitable for a printing terminal).

This bit only controls the display behavior; the ICANON bit by itself controls actual recognition of the ERASE character and erasure of input, without which ECHOE is simply irrelevant.

ECHOK

This bit enables special display of the KILL character. There are two ways this can be done. The better way is by erasing on the screen the entire line that has been killed. The worse way is by moving to a new line after echoing the KILL character normally. Some systems do one, some systems do the other, and some let you choose either way.

If this bit is not set, the KILL character echoes just as it would if it were not the KILL character. Then it is up to the user to remember that the KILL character has erased the preceding input; there is no indication of this on the screen.

This bit only controls the display behavior; the ICANON bit by itself controls actual recognition of the KILL character and erasure of input, without which ECHOK is simply irrelevant.

ECHONL

If this bit is set and the ICANON bit is also set, then the newline ('\n') character is echoed even if the ECHO bit is not set.

ISIG

This bit controls whether the INTR, QUIT, and SUSP characters are recognized. The functions associated with these characters are performed if and only if this bit is set. Being in canonical or noncanonical input mode has no affect on the interpretation of these characters.

You should use caution when disabling recognition of these characters. Programs that cannot be interrupted interactively are very user-unfriendly. If you clear this bit, your program should provide some alternate interface that allows the user to interactively send the signals associated with these characters, or to escape from the program.

See section Characters that Cause Signals.

IEXTEN

This bit is similar to ISIG, but controls implementation-defined special characters. If it is set, it might override the default behavior for the ICANON and ISIG local mode flags, and the IXON and IXOFF input mode flags.

NOFLSH

Normally, the INTR, QUIT, and SUSP characters cause input and output queues for the terminal to be cleared. If this bit is set, the queues are not cleared.

TOSTOP

If this bit is set and the system supports job control, then SIGTTOU signals are generated by background processes that attempt to write to the terminal. @xref{Access to the Terminal}.

The following bits are BSD extensions; the GNU library defines these symbols on any system if you ask for them, but the settings of the bits have no effect except on BSD systems and on the GNU system.

ECHOKE

On BSD systems, this bit selects between the two alternative ways of displaying the KILL character, when ECHOK is set. If ECHOKE is set, then the KILL character erases the whole screen line; otherwise, the KILL character moves to the next screen line. The setting of ECHOKE has no effect when ECHOK is clear.

ECHOPRT

This bit enables display of the ERASE character in a way that is geared to a hardcopy terminal.

ECHOCTL

If this bit is set, echo control characters with ‘^’ followed by the corresponding text character. Thus, control-A echoes as ‘^A’.

ALTWERASE

This bit determines how far the WERASE character should erase. The WERASE character erases back to the beginning of a word; the question is, where do words begin?

If this bit is clear, then the beginning of a word is a nonwhitespace character following a whitespace character. If the bit is set, then the beginning of a word is an alphanumeric character or underscore following a character which is none of those.

FLUSHO

This is the bit that toggles when the user types the DISCARD character. While this bit is set, all output is discarded. See section Other Special Characters.

NOKERNINFO

Setting this bit disables handling of the STATUS character. See section Other Special Characters.

PENDIN

If this bit is set, it indicates that there is a line of input that needs to be reprinted. Typing the REPRINT character sets this bit; the bit remains set until reprinting is finished. See section BSD Extensions to Editing Characters.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.4.8 Line Speed

The terminal line speed tells the computer how fast to read and write data on the terminal.

If the terminal is connected to a real serial line, the terminal speed you specify actually controls the line—if it doesn’t match the terminal’s own idea of the speed, communication does not work. Real serial ports accept only certain standard speeds. Also, particular hardware may not support even all the standard speeds. Specifying a speed of zero hangs up a dialup connection and turns off modem control signals.

If the terminal is not a real serial line (for example, if it is a network connection), then the line speed won’t really affect data transmission speed, but some programs will use it to determine the amount of padding needed. It’s best to specify a line speed value that matches the actual speed of the actual terminal, but you can safely experiment with different values to vary the amount of padding.

There are actually two line speeds for each terminal, one for input and one for output. You can set them independently, but most often terminals use the same speed for both directions.

The speed values are stored in the struct termios structure, but don’t try to access them in the struct termios structure directly. Instead, you should use the following functions to read and store them:

Function: speed_t cfgetospeed (const struct termios *termios_p)

This function returns the output line speed stored in the structure *termios_p.

Function: speed_t cfgetispeed (const struct termios *termios_p)

This function returns the input line speed stored in the structure *termios_p.

Function: int cfsetospeed (struct termios *termios_p, speed_t speed)

This function stores speed in *termios_p as the output speed. The normal return value is 0; a value of -1 indicates an error. If speed is not a speed, cfsetospeed returns -1.

Function: int cfsetispeed (struct termios *termios_p, speed_t speed)

This function stores speed in *termios_p as the input speed. The normal return value is 0; a value of -1 indicates an error. If speed is not a speed, cfsetospeed returns -1.

Function: int cfsetspeed (struct termios *termios_p, speed_t speed)

This function stores speed in *termios_p as both the input and output speeds. The normal return value is 0; a value of -1 indicates an error. If speed is not a speed, cfsetspeed returns -1. This function is an extension in 4.4 BSD.

Data Type: speed_t

The speed_t type is an unsigned integer data type used to represent line speeds.

The functions cfsetospeed and cfsetispeed report errors only for speed values that the system simply cannot handle. If you specify a speed value that is basically acceptable, then those functions will succeed. But they do not check that a particular hardware device can actually support the specified speeds—in fact, they don’t know which device you plan to set the speed for. If you use tcsetattr to set the speed of a particular device to a value that it cannot handle, tcsetattr returns -1.

Portability note: In the GNU library, the functions above accept speeds measured in bits per second as input, and return speed values measured in bits per second. Other libraries require speeds to be indicated by special codes. For POSIX.1 portability, you must use one of the following symbols to represent the speed; their precise numeric values are system-dependent, but each name has a fixed meaning: B110 stands for 110 bps, B300 for 300 bps, and so on. There is no portable way to represent any speed but these, but these are the only speeds that typical serial lines can support.

B0  B50  B75  B110  B134  B150  B200
B300  B600  B1200  B1800  B2400  B4800
B9600  B19200  B38400

BSD defines two additional speed symbols as aliases: EXTA is an alias for B19200 and EXTB is an alias for B38400. These aliases are obsolete.

Function: int cfmakeraw (struct termios *termios_p)

This function provides an easy way to set up *termios_p for what has traditionally been called “raw mode” in BSD. It does exactly this:

  termios_p->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
                  |INLCR|IGNCR|ICRNL|IXON);
  termios_p->c_oflag &= ~OPOST;
  termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
  termios_p->c_cflag &= ~(CSIZE|PARENB);
  termios_p->c_cflag |= CS8;

[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.4.9 Special Characters

In canonical input, the terminal driver recognizes a number of special characters which perform various control functions. These include the ERASE character (usually <DEL>) for editing input, and other editing characters. The INTR character (normally C-c) for sending a SIGINT signal, and other signal-raising characters, may be available in either canonical or noncanonical input mode. All these characters are described in this section.

The particular characters used are specified in the c_cc member of the struct termios structure. This member is an array; each element specifies the character for a particular role. Each element has a symbolic constant that stands for the index of that element—for example, INTR is the index of the element that specifies the INTR character, so storing '=' in termios.c_cc[INTR] specifies ‘=’ as the INTR character.

On some systems, you can disable a particular special character function by specifying the value _POSIX_VDISABLE for that role. This value is unequal to any possible character code. @xref{Options for Files}, for more information about how to tell whether the operating system you are using supports _POSIX_VDISABLE.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.4.9.1 Characters for Input Editing

These special characters are active only in canonical input mode. See section Two Styles of Input: Canonical or Not.

Macro: int VEOF

This is the subscript for the EOF character in the special control character array. termios.c_cc[VEOF] holds the character itself.

The EOF character is recognized only in canonical input mode. It acts as a line terminator in the same way as a newline character, but if the EOF character is typed at the beginning of a line it causes read to return a byte count of zero, indicating end-of-file. The EOF character itself is discarded.

Usually, the EOF character is C-d.

Macro: int VEOL

This is the subscript for the EOL character in the special control character array. termios.c_cc[VEOL] holds the character itself.

The EOL character is recognized only in canonical input mode. It acts as a line terminator, just like a newline character. The EOL character is not discarded; it is read as the last character in the input line.

You don’t need to use the EOL character to make <RET> end a line. Just set the ICRNL flag. In fact, this is the default state of affairs.

Macro: int VERASE

This is the subscript for the ERASE character in the special control character array. termios.c_cc[VERASE] holds the character itself.

The ERASE character is recognized only in canonical input mode. When the user types the erase character, the previous character typed is discarded. (If the terminal generates multibyte character sequences, this may cause more than one byte of input to be discarded.) This cannot be used to erase past the beginning of the current line of text. The ERASE character itself is discarded.

Usually, the ERASE character is <DEL>.

Macro: int VKILL

This is the subscript for the KILL character in the special control character array. termios.c_cc[VKILL] holds the character itself.

The KILL character is recognized only in canonical input mode. When the user types the kill character, the entire contents of the current line of input are discarded. The kill character itself is discarded too.

The KILL character is usually C-u.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.4.9.2 BSD Extensions to Editing Characters

These special characters are active only in canonical input mode. See section Two Styles of Input: Canonical or Not. They are BSD extensions; the GNU library defines the symbols on any system if you ask for them, but the characters you specify don’t actually do anything except on a BSD system.

Macro: int VEOL2

This is the subscript for the EOL2 character in the special control character array. termios.c_cc[VEOL2] holds the character itself.

The EOL2 character works just like the EOL character (see above), but it can be a different character. Thus, you can specify two characters to terminate an input line, but setting EOL to one of them and EOL2 to the other.

Macro: int VWERASE

This is the subscript for the WERASE character in the special control character array. termios.c_cc[VWERASE] holds the character itself.

The WERASE character is recognized only in canonical input mode. It erases an entire word of prior input.

Macro: int VREPRINT

This is the subscript for the REPRINT character in the special control character array. termios.c_cc[VREPRINT] holds the character itself.

The REPRINT character is recognized only in canonical input mode. It reprints the current input line.

Macro: int VLNEXT

This is the subscript for the LNEXT character in the special control character array. termios.c_cc[VLNEXT] holds the character itself.

The LNEXT character is recognized only when IEXTEN is set. It disables the editing significance of the next character the user types. It is the analogue of the C-q command in Emacs. “LNEXT” stands for “literal next.”

The LNEXT character is usually C-v.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.4.9.3 Characters that Cause Signals

These special characters may be active in either canonical or noncanonical input mode, but only when the ISIG flag is set (see section Local Modes).

Macro: int VINTR

This is the subscript for the INTR character in the special control character array. termios.c_cc[VINTR] holds the character itself.

The INTR (interrupt) character raises a SIGINT signal for all processes in the foreground job associated with the terminal. The INTR character itself is then discarded. @xref{Signal Handling}, for more information about signals.

Typically, the INTR character is C-c.

Macro: int VQUIT

This is the subscript for the QUIT character in the special control character array. termios.c_cc[VQUIT] holds the character itself.

The QUIT character raises a SIGQUIT signal for all processes in the foreground job associated with the terminal. The QUIT character itself is then discarded. @xref{Signal Handling}, for more information about signals.

Typically, the QUIT character is C-\.

Macro: int VSUSP

This is the subscript for the SUSP character in the special control character array. termios.c_cc[VSUSP] holds the character itself.

The SUSP (suspend) character is recognized only if the implementation supports job control (@pxref{Job Control}). It causes a SIGTSTP signal to be sent to all processes in the foreground job associated with the terminal. The SUSP character itself is then discarded. @xref{Signal Handling}, for more information about signals.

Typically, the SUSP character is C-z.

Few applications disable the normal interpretation of the SUSP character. If your program does this, it should provide some other mechanism for the user to stop the job. When the user invokes this mechanism, the program should send a SIGTSTP signal to the process group of the process, not just to the process itself. @xref{Signaling Another Process}.

Macro: int VDSUSP

This is the subscript for the DSUSP character in the special control character array. termios.c_cc[VDSUSP] holds the character itself.

The DSUSP (suspend) character is recognized only if the implementation supports job control (@pxref{Job Control}). It sends a SIGTSTP signal, like the SUSP character, but not right away—only when the program tries to read it as input. Not all systems with job control support DSUSP; only BSD systems.

@xref{Signal Handling}, for more information about signals.

Typically, the DSUSP character is C-y.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.4.9.4 Special Characters for Flow Control

These special characters may be active in either canonical or noncanonical input mode, but their use is controlled by the flags IXON and IXOFF (see section Input Modes).

Macro: int VSTART

This is the subscript for the START character in the special control character array. termios.c_cc[VSTART] holds the character itself.

The START character is used to support the IXON and IXOFF input modes. If IXON is set, receiving a START character resumes suspended output; the START character itself is discarded. If IXOFF is set, the system may also transmit START characters to the terminal.

The usual value for the START character is C-q. You may not be able to change this value—the hardware may insist on using C-q regardless of what you specify.

Macro: int VSTOP

This is the subscript for the STOP character in the special control character array. termios.c_cc[VSTOP] holds the character itself.

The STOP character is used to support the IXON and IXOFF input modes. If IXON is set, receiving a STOP character causes output to be suspended; the STOP character itself is discarded. If IXOFF is set, the system may also transmit STOP characters to the terminal, to prevent the input queue from overflowing.

The usual value for the STOP character is C-s. You may not be able to change this value—the hardware may insist on using C-s regardless of what you specify.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.4.9.5 Other Special Characters

Here are two additional special characters that are meaningful on BSD systems.

Macro: int VDISCARD

This is the subscript for the DISCARD character in the special control character array. termios.c_cc[VDISCARD] holds the character itself.

The DISCARD character is recognized only when IEXTEN is set. Its effect is to toggle the discard-output flag. When this flag is set, all program output is discarded. Setting the flag also discards all output currently in the output buffer.

Macro: int VSTATUS

This is the subscript for the STATUS character in the special control character array. termios.c_cc[VSTATUS] holds the character itself.

The STATUS character’s effect is to print out a status message about how the current process is running.

The STATUS character is recognized only when canonical mode. This is a peculiar design decision, since the STATUS character’s meaning has nothing to do with input, but that’s the way it was done.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.4.10 Noncanonical Input

In noncanonical input mode, the special editing characters such as ERASE and KILL are ignored. The system facilities for the user to edit input are disabled in noncanonical mode, so that all input characters (unless they are special for signal or flow-control purposes) are passed to the application program exactly as typed. It is up to the application program to give the user ways to edit the input, if appropriate.

Noncanonical mode offers special parameters called MIN and TIME for controlling whether and how long to wait for input to be available. You can even use them to avoid ever waiting—to return immediately with whatever input is available, or with no input.

The MIN and TIME are stored in elements of the c_cc array, which is a member of the struct termios structure. Each element of this array has a particular role, and each element has a symbolic constant that stands for the index of that element. VMIN and VMAX are the names for the indices in the array of the MIN and TIME slots.

Macro: int VMIN

This is the subscript for the MIN slot in the c_cc array. Thus, termios.c_cc[VMIN] is the value itself.

The MIN slot is only meaningful in noncanonical input mode; it specifies the minimum number of bytes that must be available in the input queue in order for read to return.

Macro: int VTIME

This is the subscript for the TIME slot in the c_cc array. Thus, termios.c_cc[VTIME] is the value itself.

The TIME slot is only meaningful in noncanonical input mode; it specifies how long to wait for input before returning, in units of 0.1 seconds.

The MIN and TIME values interact to determine the criterion for when read should return; their precise meanings depend on which of them are nonzero. There are four possible cases:

What happens if MIN is 50 and you ask to read just 10 bytes? Normally, read waits until there are 50 bytes in the buffer (or, more generally, the wait condition described above is satisfied), and then reads 10 of them, leaving the other 40 buffered in the operating system for a subsequent call to read.

Portability note: On some systems, the MIN and TIME slots are actually the same as the EOF and EOL slots. This causes no serious problem because the MIN and TIME slots are used only in noncanonical input and the EOF and EOL slots are used only in canonical input, but it isn’t very clean. The GNU library allocates separate slots for these uses.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.5 Line Control Functions

These functions perform miscellaneous control actions on terminal devices. As regards terminal access, they are treated like doing output: if any of these functions is used by a background process on its controlling terminal, normally all processes in the process group are sent a SIGTTOU signal. The exception is if the calling process itself is ignoring or blocking SIGTTOU signals, in which case the operation is performed and no signal is sent. @xref{Job Control}.

Function: int tcsendbreak (int filedes, int duration)

This function generates a break condition by transmitting a stream of zero bits on the terminal associated with the file descriptor filedes. The duration of the break is controlled by the duration argument. If zero, the duration is between 0.25 and 0.5 seconds. The meaning of a nonzero value depends on the operating system.

This function does nothing if the terminal is not an asynchronous serial data port.

The return value is normally zero. In the event of an error, a value of -1 is returned. The following errno error conditions are defined for this function:

EBADF

The filedes is not a valid file descriptor.

ENOTTY

The filedes is not associated with a terminal device.

Function: int tcdrain (int filedes)

The tcdrain function waits until all queued output to the terminal filedes has been transmitted.

The return value is normally zero. In the event of an error, a value of -1 is returned. The following errno error conditions are defined for this function:

EBADF

The filedes is not a valid file descriptor.

ENOTTY

The filedes is not associated with a terminal device.

EINTR

The operation was interrupted by delivery of a signal. @xref{Interrupted Primitives}.

Function: int tcflush (int filedes, int queue)

The tcflush function is used to clear the input and/or output queues associated with the terminal file filedes. The queue argument specifies which queue(s) to clear, and can be one of the following values:

TCIFLUSH

Clear any input data received, but not yet read.

TCOFLUSH

Clear any output data written, but not yet transmitted.

TCIOFLUSH

Clear both queued input and output.

The return value is normally zero. In the event of an error, a value of -1 is returned. The following errno error conditions are defined for this function:

EBADF

The filedes is not a valid file descriptor.

ENOTTY

The filedes is not associated with a terminal device.

EINVAL

A bad value was supplied as the queue argument.

It is unfortunate that this function is named tcflush, because the term “flush” is normally used for quite another operation—waiting until all output is transmitted—and using it for discarding input or output would be confusing. Unfortunately, the name tcflush comes from POSIX and we cannot change it.

Function: int tcflow (int filedes, int action)

The tcflow function is used to perform operations relating to XON/XOFF flow control on the terminal file specified by filedes.

The action argument specifies what operation to perform, and can be one of the following values:

TCOOFF

Suspend transmission of output.

TCOON

Restart transmission of output.

TCIOFF

Transmit a STOP character.

TCION

Transmit a START character.

For more information about the STOP and START characters, see Special Characters.

The return value is normally zero. In the event of an error, a value of -1 is returned. The following errno error conditions are defined for this function:

EBADF

The filedes is not a valid file descriptor.

ENOTTY

The filedes is not associated with a terminal device.

EINVAL

A bad value was supplied as the action argument.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.6 Noncanonical Mode Example

Here is an example program that shows how you can set up a terminal device to read single characters in noncanonical input mode, without echo.

This program is careful to restore the original terminal modes before exiting or terminating with a signal. It uses the atexit function (@pxref{Cleanups on Exit}) to make sure this is done by exit.

The shell is supposed to take care of resetting the terminal modes when a process is stopped or continued; see @ref{Job Control}. But some existing shells do not actually do this, so you may wish to establish handlers for job control signals that reset terminal modes. The above example does so.


[Top] [Contents] [Index] [ ? ]

About This Document

This document was generated on March 29, 2022 using texi2html 5.0.

The buttons in the navigation panels have the following meaning:

Button Name Go to From 1.2.3 go to
[ << ] FastBack Beginning of this chapter or previous chapter 1
[ < ] Back Previous section in reading order 1.2.2
[ Up ] Up Up section 1.2
[ > ] Forward Next section in reading order 1.2.4
[ >> ] FastForward Next chapter 2
[Top] Top Cover (top) of document  
[Contents] Contents Table of contents  
[Index] Index Index  
[ ? ] About About (help)  

where the Example assumes that the current position is at Subsubsection One-Two-Three of a document of the following structure:


This document was generated on March 29, 2022 using texi2html 5.0.